package com.systop.scos.news.webapp;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.hibernate.criterion.MatchMode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.systop.common.modules.fileattch.model.FileAttch;
import com.systop.common.modules.fileattch.service.FileAttchManager;
import com.systop.common.modules.security.user.LoginUserService;
import com.systop.core.dao.support.Page;
import com.systop.core.util.PageUtil;
import com.systop.core.webapp.struts2.action.DefaultCrudAction;
import com.systop.scos.news.model.News;
import com.systop.scos.news.model.NewsComment;
import com.systop.scos.news.service.NewsCommentManager;
import com.systop.scos.news.service.NewsManager;
import com.systop.scos.news.service.NewsTypeManager;

/**
 * 新闻action
 * 
 * @author yj
 * 
 */
@SuppressWarnings({ "serial", "unchecked", "rawtypes" })
@Controller
@Scope(BeanDefinition.SCOPE_PROTOTYPE)
public class NewsAction extends DefaultCrudAction<News, NewsManager> {

	/**
	 * 查询起始时间
	 */
	private Date startTime;

	/**
	 * 查询截至时间
	 */
	private Date endTime;

	/**
	 * 登陆用户信息管理
	 */
	@Autowired
	private LoginUserService loginUserService;

	/**
	 * 附件管理
	 */
	@Autowired
	private FileAttchManager fileAttchManager;

	/**
	 * 新闻类别管理
	 */
	@Autowired
	private NewsTypeManager newsTypeManager;

	/**
	 * 新闻评论管理
	 */
	@Autowired
	private NewsCommentManager newsCommentManager;

	/**
	 * 保存新闻
	 */
	public String save() {
		if (getModel().getId() == null) {
			getModel().setCreateTime(new Date());
			// 设置发布人
			getModel().setAuthor(loginUserService.getLoginUser(getRequest()));
		}
		if (getModel().getHits() == null) {
			getModel().setHits(0);
		}
		getModel().setUpdateTime(new Date());
		getManager().getDao().clear();
		getManager().save(getModel());
		return SUCCESS;
	}

	/**
	 * 新闻查询列表
	 */
	@Override
	public String index() {
		Page page = PageUtil.getPage(getPageNo(), getPageSize());
		StringBuffer sql = new StringBuffer("from News n where 1=1 ");
		List args = new ArrayList();

		if (StringUtils.isNotBlank(getModel().getTitle())) {
			sql.append(" and n.title like ?");
			args.add(MatchMode.ANYWHERE.toMatchString(getModel().getTitle()));
		}

		if (startTime != null && endTime != null) {
			sql.append(" and n.createTime >= ? and n.createTime <= ? ");
			args.add(startTime);
			args.add(endTime);
		}
		sql.append(" order by n.createTime desc");
		page = getManager().pageQuery(page, sql.toString(), args.toArray());
		restorePageData(page);
		return INDEX;
	}

	/**
	 * 新闻查看
	 */
	public String view() {
		getModel().setHits(getModel().getHits() + 1);
		getManager().save(getModel());
		return VIEW;
	}

	/**
	 * 删除新闻及日志附件信息
	 */
	@Override
	public String remove() {
		if (getModel().getId() != null) {
			News news = getManager().get(getModel().getId());
			if (StringUtils.isNotBlank(news.getAttachIds())) {
				// 更新附件表的Type为日志表的附件
				String[] test = news.getAttachIds().split(",");
				for (int i = 0; i < test.length; i++) {
					FileAttch fileAttch = fileAttchManager.getFileAttch(Integer
							.valueOf(test[i]));
					if (fileAttch != null) {
						fileAttchManager.remove(fileAttch);
					}
				}
			}
			// 删除该新闻所有的评论信息
			if (CollectionUtils.isNotEmpty(news.getNewsComments())) {
				for (NewsComment newsComment : news.getNewsComments()) {
					newsCommentManager.remove(newsComment);
				}
			}
			getManager().remove(news);
		}
		return SUCCESS;
	}

	/**
	 * 显示新闻类别
	 */
	public Map getNewsTypeMap() {
		return newsTypeManager.getNewsTypeMap();
	}

	/**
	 * 
	 * @return
	 */
	public Date getStartTime() {
		return startTime;
	}

	public void setStartTime(Date startTime) {
		this.startTime = startTime;
	}

	public Date getEndTime() {
		return endTime;
	}

	public void setEndTime(Date endTime) {
		this.endTime = endTime;
	}

}
